package session07.gui;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {

    private Container contentP;//内容面板

    private JLabel txtLab;//文字标签
    private JLabel imgLab;//图片标签

    private JTextField nameTxt;//文本框
    private JPasswordField pwdTxt;//密码框

    private JButton exitBtn;//文字按钮
    private JButton loginBtn;//文字+图标按钮
    private JButton clearBtn;//图片按钮

    private JRadioButton maleBtn;
    private JRadioButton femaleBtn;

    private JCheckBox readCheck;
    private JCheckBox sportCheck;
    private JCheckBox musicCheck;

    private JComboBox<String> classComb;

    private JTextArea msgTxt;

    public MyFrame(){
        this.setSize(600,500); //设置宽、高
        //方法一：
        Toolkit tk = Toolkit.getDefaultToolkit();
//        double screenWidth = tk.getScreenSize().getWidth();
//        double screenHeight = tk.getScreenSize().getHeight();
//        jFrame.setLocation((int)(screenWidth - jFrame.getWidth()) / 2,
//                (int)(screenHeight - jFrame.getHeight()) / 2);//设置左上角距离上层容器左上角的x和y坐标
        //方法二：
        this.setLocationRelativeTo(null);//传另一个容器就相对于另一个容器，传null就相对于屏幕
        this.setTitle("我的第一个窗体");
        this.setIconImage(tk.createImage("img/hp.JPG"));//设置窗体的图片
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//设置关闭窗口即关闭程序
        this.setResizable(false);//设置窗体大小不可变

        this.addContent();

        this.setVisible(true);//设置界面可见，永远放到最后
    }

    private void addContent(){
        //1、获取内容面板
        this.contentP = this.getContentPane();
        this.contentP.setBackground(Color.WHITE);//设置内容面板的背景色
        this.contentP.setLayout(null);//使用null布局，所有组件或子容器采用绝对布局的方式

        //2、添加组件
        this.txtLab = new JLabel();
        this.txtLab.setText("请输入用户名：");
        this.txtLab.setBounds(10,10,150,50);
        this.txtLab.setFont(new Font("宋体",Font.ITALIC,20));//设置字体
        this.txtLab.setForeground(Color.GREEN);//设置前景色
        this.txtLab.setBorder(BorderFactory.createLineBorder(Color.BLUE));//设置外边框
        this.contentP.add(this.txtLab);

        this.imgLab = new JLabel();
        this.imgLab.setIcon(new ImageIcon("img/title.jpg"));
        this.imgLab.setBounds(10,70,324,49);
        this.contentP.add(this.imgLab);

        this.nameTxt = new JTextField();
        this.nameTxt.setBounds(170,10,150,30);
        this.nameTxt.setEditable(true);//设置是否可编辑
        this.contentP.add(this.nameTxt);

        this.pwdTxt = new JPasswordField();
        this.pwdTxt.setBounds(350,70,150,30);
        this.pwdTxt.setEchoChar('*');//设置密码框的替换文本
        this.contentP.add(this.pwdTxt);

        this.exitBtn = new JButton();
        this.exitBtn.setText("退出");
        this.exitBtn.setBounds(10,130,80,30);
        this.exitBtn.setEnabled(false);//按钮是否能用
        this.contentP.add(this.exitBtn);

        this.loginBtn = new JButton();
        this.loginBtn.setText("登陆");
        this.loginBtn.setIcon(new ImageIcon("img/hp.JPG"));
        this.loginBtn.setBounds(100,130,80,30);
        this.loginBtn.setRolloverIcon(new ImageIcon("img/logo.gif"));//鼠标进入后的图标设置
        this.contentP.add(this.loginBtn);

        this.clearBtn = new JButton();
        this.clearBtn.setIcon(new ImageIcon("img/buttonClear.jpg"));
        this.clearBtn.setBounds(200,130,140,50);
        this.contentP.add(this.clearBtn);

        this.maleBtn = new JRadioButton("男");
        this.femaleBtn = new JRadioButton("女");
        this.femaleBtn.setSelected(true);
        this.maleBtn.setBounds(10,190,50,30);
        this.femaleBtn.setBounds(80,190,50,30);
        this.contentP.add(this.maleBtn);
        this.contentP.add(this.femaleBtn);
        //单选按钮需要逻辑分组，才能够有互斥效果
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(this.maleBtn);
        btnGroup.add(this.femaleBtn);

        this.readCheck = new JCheckBox();
        this.readCheck.setText("阅读");
        this.sportCheck = new JCheckBox();
        this.sportCheck.setText("运动");
        this.musicCheck = new JCheckBox("音乐");
        this.musicCheck.setSelected(true);
        this.readCheck.setBounds(150,240,80,30);
        this.sportCheck.setBounds(250,240,80,30);
        this.musicCheck.setBounds(350,240,80,30);
        this.contentP.add(this.readCheck);
        this.contentP.add(this.sportCheck);
        this.contentP.add(this.musicCheck);

        this.classComb = new JComboBox<>();
        this.classComb.addItem("公共1班");
        this.classComb.addItem("公共2班");
        this.classComb.addItem("公共3班");
        this.classComb.addItem("公共4班");
//        this.classComb.setSelectedIndex(1);//设置默认的选项下标
        this.classComb.setEditable(true);//设置下拉框可编辑
        this.classComb.setBounds(420,240,150,40);
        this.contentP.add(this.classComb);

        this.msgTxt = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(this.msgTxt);
        scrollPane.setBounds(10,300,400,150);
        this.contentP.add(scrollPane);
    }
}
